package com.tuanzi.loan.business.repository.statistics.impl;

import com.tuanzi.loan.business.repository.statistics.BigClueStatRepository;
import com.tuanzi.loan.core.repository.JOOQBaseRepositoryPlus;
import com.tuanzi.loan.vo.request.statistics.BigClueStatSearchRequest;
import com.tuanzi.loan.vo.response.statistics.BigClueStatSearchResponse;
import org.jooq.impl.DSL;
import org.jooq.impl.TableImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;

import static com.tuanzi.loan.generated.jooq.stat.tables.StatBigClue.STAT_BIG_CLUE;

/**
* @Description:    大额线索漏斗统计实现类
* @Author:         xionghao
* @CreateDate:     2018-07-27
*/
@SuppressWarnings({ "rawtypes" })
@Repository
public class BigClueStatRepositoryImpl extends JOOQBaseRepositoryPlus implements BigClueStatRepository{
    @Override
    public Page<BigClueStatSearchResponse> list(BigClueStatSearchRequest searchRequest, Pageable pageable) {
        if ("-1000".equals(searchRequest.getCfrom())){
            fields(STAT_BIG_CLUE.TIME,
                DSL.sum(STAT_BIG_CLUE.SUCCESS_CLUE_SHOW_UV).as("success_clue_show_uv"),
                DSL.sum(STAT_BIG_CLUE.SUCCESS_CLUE_CLICK_UV).as("success_clue_click_uv"),
                DSL.sum(STAT_BIG_CLUE.JIAFANG_CLUE_SHOW_UV).as("jiafang_clue_show_uv"),
                DSL.sum(STAT_BIG_CLUE.JIAFANG_CLUE_CLICK_UV).as("jiafang_clue_click_uv"),
                DSL.sum(STAT_BIG_CLUE.HFIVE_CLUE_SHOW_UV).as("hfive_clue_show_uv"),
                DSL.sum(STAT_BIG_CLUE.HFIVE_CLUE_CLICK_UV).as("hfive_clue_click_uv")
            );
        }else{
            fields(STAT_BIG_CLUE.fields());
        }
        if (!"-1000".equals(searchRequest.getCfrom())){
            if ("菠萝贷站内".equals(searchRequest.getCfrom())){
                eq(STAT_BIG_CLUE.CFROM,"null");
            }else{
                eq(STAT_BIG_CLUE.CFROM,searchRequest.getCfrom());
            }
        }
        if (Objects.nonNull(searchRequest.getStartdate())){
            ge(STAT_BIG_CLUE.TIME,searchRequest.getStartdate());
        }
        if (Objects.nonNull(searchRequest.getEnddate())){
            le(STAT_BIG_CLUE.TIME,searchRequest.getEnddate());
        }
        if ("-1000".equals(searchRequest.getCfrom())){
            groupBy(STAT_BIG_CLUE.TIME);
        }
        orderBy(STAT_BIG_CLUE.TIME, Sort.Direction.DESC);
        return search(pageable,record -> {
            BigClueStatSearchResponse response = new BigClueStatSearchResponse();
            if ("-1000".equals(searchRequest.getCfrom())){
                response.setCfrom("全部");
            }else{
                if ("null".equals(record.get(STAT_BIG_CLUE.CFROM))){
                    response.setCfrom("菠萝贷站内");
                }else{
                    response.setCfrom(record.get(STAT_BIG_CLUE.CFROM));
                }
            }

            response.setTime(record.get(STAT_BIG_CLUE.TIME));
            response.setHfive_clue_click_uv(dealNullPoint(record.get(STAT_BIG_CLUE.HFIVE_CLUE_CLICK_UV,BigDecimal.class)));
            response.setHfive_clue_show_uv(dealNullPoint(record.get(STAT_BIG_CLUE.HFIVE_CLUE_SHOW_UV,BigDecimal.class)));
            response.setJiafang_clue_click_uv(dealNullPoint(record.get(STAT_BIG_CLUE.JIAFANG_CLUE_CLICK_UV,BigDecimal.class)));
            response.setJiafang_clue_show_uv(dealNullPoint(record.get(STAT_BIG_CLUE.JIAFANG_CLUE_SHOW_UV,BigDecimal.class)));
            response.setSuccess_clue_click_uv(dealNullPoint(record.get(STAT_BIG_CLUE.SUCCESS_CLUE_CLICK_UV,BigDecimal.class)));
            response.setSuccess_clue_show_uv(dealNullPoint(record.get(STAT_BIG_CLUE.SUCCESS_CLUE_SHOW_UV,BigDecimal.class)));
            return response;
        });
    }

    @Override
    public List<String> returnCfrom() {
        List<String> resultList =
                dslContext.selectDistinct(STAT_BIG_CLUE.CFROM).from(STAT_BIG_CLUE)
                        .fetch(STAT_BIG_CLUE.CFROM);
        return resultList;
    }

    private Integer dealNullPoint(BigDecimal object){
        if (object!=null){
            return object.intValue();
        }
        return 0;
    }
    @Override
    protected TableImpl getTable() {
        return STAT_BIG_CLUE;
    }

}
